<?php

namespace Biz\service\BrandActivity;

use Biz\model\brandActivity\BlindBoxActivity;
use Biz\model\brandActivity\BrandActivityBasic;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\model\NewProduct\LogisticsLogModel;
use Biz\model\NewProduct\NewProductWikiModel;
use Biz\service\GoodsTestActivity\FineGoodsService;
use Biz\service\NewProduct\NewProductWinnerListService;

class BrandActivityService
{
    public static function index($where): array
    {
        $data = BrandActivityBasic::selectByWhere($where);
        $goodsTestIdArr = array_column(FineGoodsService::arrayFilterFieldValue($data, 'activity_type',
            BrandActivityBasic::ACTIVITY_TYPE_LIGHT_TEST), 'activity_id');
        if ($goodsTestIdArr) {
            $goodsTestMap = NewProductWinnerListService::handleData(FineGoodsModel::selectList(['id' => $goodsTestIdArr],
                ['activity_title', 'id']), 'id');
        }
        $newProductIdArr = array_column(FineGoodsService::arrayFilterFieldValue($data, 'activity_type',
            BrandActivityBasic::ACTIVITY_TYPE_NEW_PRODUCT), 'activity_id');
        if ($newProductIdArr) {
            $newProductMap = NewProductWinnerListService::handleData(NewProductWikiModel::select(['name', 'id'],
                ['id' => $newProductIdArr]), 'id');
        }
        $blindBoxIdArr = array_column(FineGoodsService::arrayFilterFieldValue($data, 'activity_type',
            BrandActivityBasic::ACTIVITY_TYPE_BLIND_BOX), 'activity_id');
        if ($blindBoxIdArr) {
            $blindBoxMap = NewProductWinnerListService::handleData(BlindBoxActivity::select(['title', 'id'],
                ['id' => $blindBoxIdArr]), 'id');
        }
        $res = [];
        if ($data) {
            foreach ($data as $val) {
                if ($val['activity_type'] == BrandActivityBasic::ACTIVITY_TYPE_BLIND_BOX) {
                    $val['activity_name'] = $blindBoxMap[$val['activity_id']]['title'] ?? '';
                }
                if ($val['activity_type'] == BrandActivityBasic::ACTIVITY_TYPE_NEW_PRODUCT) {
                    $val['activity_name'] = $newProductMap[$val['activity_id']]['name'] ?? '';
                }
                if ($val['activity_type'] == BrandActivityBasic::ACTIVITY_TYPE_LIGHT_TEST) {
                    $val['activity_name'] = $goodsTestMap[$val['activity_id']]['activity_title'] ?? '';
                }
                $res[] = $val;
            }
        }

        return $res;
    }

    /**
     * @param $where
     * @return array|bool
     */
    public static function logisticsIndex($where)
    {
        return LogisticsLogModel::select('*',$where) ?? [];
    }
}